package com.example.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.SysConst;
import com.example.web.dto.*;
import com.example.web.dto.query.*;
import com.example.web.entity.*;
import com.example.web.mapper.*;
import com.example.web.service.*;
import com.example.web.tools.dto.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;
import com.example.web.tools.*;
/**
 * 商品功能实现类
 */
@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements GoodService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的Good表mapper对象
     */
    @Autowired
    private GoodMapper _GoodMpper;

 			
    @Autowired
    private GoodTypeMapper _GoodTypeMapper;                        
			

    /**
     * 商品分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<GoodDto> List(GoodPagedInput input) {

      
        //声明一个支持商品查询的(拉姆达)表达式
        LambdaQueryWrapper<Good> queryWrapper = Wrappers.<Good>lambdaQuery()
                .eq(input.getId()!=null&&input.getId()!=0,Good::getId,input.getId())
                .eq(input.getCreatorId()!=null,Good::getCreatorId,input.getCreatorId());
      
     	 //如果前端搜索传入Name不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getName())) {
            queryWrapper=queryWrapper.like(Good::getName,input.getName());
       	 }
     	 //如果前端搜索传入Unit不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getUnit())) {
            queryWrapper=queryWrapper.eq(Good::getUnit,input.getUnit());
       	 }
     	 //如果前端搜索传入PriceStartRange,则要大于PriceStartRange输入的值
         if(input.getPriceStartRange()!=null)
          {
            queryWrapper=queryWrapper.gt(Good::getPrice,input.getPriceStartRange());
          }
         //如果前端搜索传入PriceEndRange,则要小于PriceEndRange输入的值
      		 if(input.getPriceEndRange()!=null)
          {
            queryWrapper=queryWrapper.lt(Good::getPrice,input.getPriceEndRange());
          }
     	 //如果前端搜索传入StockStartRange,则要大于StockStartRange输入的值
         if(input.getStockStartRange()!=null)
          {
            queryWrapper=queryWrapper.gt(Good::getStock,input.getStockStartRange());
          }
         //如果前端搜索传入StockEndRange,则要小于StockEndRange输入的值
      		 if(input.getStockEndRange()!=null)
          {
            queryWrapper=queryWrapper.lt(Good::getStock,input.getStockEndRange());
          }
     	 //如果前端搜索传入GoodTypeId不为空,则进行精确查询
        if(input.getGoodTypeId()!=null) {
            queryWrapper=queryWrapper.eq(Good::getGoodTypeId,input.getGoodTypeId());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(Good::getCreationTime);
        //构建一个分页查询的model
        Page<Good> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取商品数据
        IPage<Good> pageRecords= _GoodMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _GoodMpper.selectCount(queryWrapper);

        //把Good实体转换成Good传输模型
        List<GoodDto> items= Extension.copyBeanList(pageRecords.getRecords(),GoodDto.class);
        for (GoodDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto  CreatorAppUserDTO=new AppUserDto();
            AppUser  CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
           
          	            
           //查询出关联的GoodType表信息
           GoodTypeDto GoodTypeDTO = new GoodTypeDto();        
            GoodType  GoodTypeEntity= _GoodTypeMapper.selectOne(Wrappers.<GoodType>lambdaQuery().eq(GoodType::getId,item.getGoodTypeId()));
           if(GoodTypeEntity!=null) {  
          BeanUtils.copyProperties(GoodTypeDTO,GoodTypeEntity);
            item.setGoodTypeDto(GoodTypeDTO); 
           }
          
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个商品查询
     */
    @SneakyThrows
    @Override
    public GoodDto Get(GoodPagedInput input) {
       if(input.getId()==null)
        {
            return new GoodDto();
        }
        PagedResult<GoodDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new GoodDto();
    }

    /**
     *商品创建或者修改
     */
    @SneakyThrows
    @Override
    public GoodDto CreateOrEdit(GoodDto input) {

        //声明一个商品实体
        Good Good = new Good();

        //把前端传入的input参数拷贝到商品实体
        BeanUtils.copyProperties(Good,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(Good);

        //定义一个返回给前端的商品传输模型
        GoodDto GoodDto=new GoodDto();

        //同理把操作的商品实体拷贝给商品传输模型
        BeanUtils.copyProperties(GoodDto,Good);

        //把传输模型返回给前端
        return GoodDto;
    }



}
